package com.ljfiction.book.fragment.ranking;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ProgressBar;

import com.bumptech.glide.Glide;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.google.gson.Gson;
import com.google.gson.JsonSyntaxException;
import com.ljfiction.book.R;
import com.ljfiction.book.MyView.DividerItemDecoration;
import com.ljfiction.book.activitys.BookDetailsActivity;
import com.ljfiction.book.fragment.BaseFragment;
import com.ljfiction.book.http.HttpManager;
import com.ljfiction.book.tools.SettingManager;
import com.ljfiction.book.tools.StringTool;
import com.ljfiction.book.tools.TimeTool;
import com.lzy.okhttputils.OkHttpUtils;
import com.lzy.okhttputils.callback.StringCallback;
import com.lzy.okhttputils.request.BaseRequest;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadMoreListener;
import com.zhy.autolayout.utils.AutoUtils;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;
import okhttp3.Call;
import okhttp3.Response;

/**
 * Created by Android on 2017/12/7.
 */

public class RankNotatleFragment extends BaseFragment {

    private SmartRefreshLayout swipeRefresh;
    private RecyclerView ranknotatalRacycle;
    private int type;
    private Context context;
    private int page = 1;
    private BaseQuickAdapter mAdapter;
    private List<RankingPareantBean> showBeanList = new ArrayList<>();
    private ProgressBar loding;
    private int zyz = 0;//0是新书，1是周，2是月，3是总；

    public static RankNotatleFragment newInstance(Context context, int type, int zyz) {
        RankNotatleFragment newFragment = new RankNotatleFragment();
        Bundle bundle = new Bundle();
        bundle.putInt("type", type);
        bundle.putInt("zyz", zyz);
        newFragment.setArguments(bundle);
        return newFragment;

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        super.onCreateView(inflater, container, savedInstanceState);
        View view = inflater.inflate(R.layout.ranking_notatlefragment, container, false);
        Bundle args = getArguments();
        if (args != null) {
            this.context = getContext();
            this.type = args.getInt("type");
            this.zyz = args.getInt("zyz");
        }
        swipeRefresh= (SmartRefreshLayout) view.findViewById(R.id.swipe_refresh);
        ranknotatalRacycle= (RecyclerView) view.findViewById(R.id.ranknotatal_racycle);
        loding= (ProgressBar) view.findViewById(R.id.progressBar1);
        intView();
        intData();
        return view;
    }

    private void intView() {
        mAdapter = new BaseQuickAdapter<RankingPareantBean, BaseViewHolder>(R.layout.ranking_list_item, showBeanList) {
            @Override
            protected void convert(BaseViewHolder helper, RankingPareantBean item) {
                AutoUtils.autoSize(helper.itemView);
                Glide.with(context).load(item.getBookImage()).into((ImageView) helper.getView(R.id.rankingitem_imag));
                switch (helper.getLayoutPosition()) {
                    case 0:
                        helper.getView(R.id.numerone).setVisibility(View.VISIBLE);
                        ((ImageView) helper.getView(R.id.numerone)).setImageResource(R.mipmap.gold);
                        break;
                    case 1:
                        helper.getView(R.id.numerone).setVisibility(View.VISIBLE);
                        ((ImageView) helper.getView(R.id.numerone)).setImageResource(R.mipmap.silver);
                        break;
                    case 2:
                        helper.getView(R.id.numerone).setVisibility(View.VISIBLE);
                        ((ImageView) helper.getView(R.id.numerone)).setImageResource(R.mipmap.copper);
                        break;
                    default:
                        helper.getView(R.id.numerone).setVisibility(View.GONE);
                        break;
                }
                helper.setText(R.id.rankingitem_bookname, item.getBookName())
                        .setText(R.id.rankingitem_auther, "作者: " + item.getWriterName())
                        .setText(R.id.rankingitem_synopsis, item.getBookIntroduction());
                switch (type){
                    case 1:
                        helper.setText(R.id.jinpiaonum, StringTool.getNum(item.getTempTicketSum())+"金椒");
                        break;
                    case 2:
                        helper.setText(R.id.jinpiaonum, StringTool.getNum(item.getTempTicketSum())+"小米椒");
                        break;
                    case 3:
                        helper.setText(R.id.jinpiaonum, StringTool.getClicknum(item.getTempTicketSum())+"点击");
                        break;
                    case 4:
                        helper.getView(R.id.jinpiaonum).setVisibility(View.GONE);
                        break;
                    case 5:
                        helper.getView(R.id.jinpiaonum).setVisibility(View.GONE);

                        break;
                    case 6:
                        helper.setText(R.id.jinpiaonum, StringTool.getNum(item.getTempTicketSum())+"吐槽");
                        break;
                    case 7:
                        helper.setText(R.id.jinpiaonum, TimeTool.getDateTime(item.getLastUpdateTime()));
                        break;
                    case 8:
                        helper.setText(R.id.jinpiaonum, StringTool.getNum(item.getTempTicketSum())+"投喂辣椒");
                        break;

                }
            }
        };
        mAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                Intent intent=new Intent(context, BookDetailsActivity.class);
                intent.putExtra("bookId",String.valueOf(showBeanList.get(position).getBookId()));
                startActivity(intent);
            }
        });
        ranknotatalRacycle.setAdapter(mAdapter);
        ranknotatalRacycle.setLayoutManager(new LinearLayoutManager(context));
        ranknotatalRacycle.addItemDecoration(new DividerItemDecoration(context, DividerItemDecoration.VERTICAL_LIST));
        swipeRefresh.setOnRefreshLoadMoreListener(new OnRefreshLoadMoreListener() {
            @Override
            public void onLoadMore(RefreshLayout refreshlayout) {
                page++;
                intData();
            }

            @Override
            public void onRefresh(RefreshLayout refreshlayout) {
                page = 1;
                showBeanList.clear();
                intData();
            }
        });
    }

    private void intData() {
        OkHttpUtils.post(HttpManager.IP + "/ranking-book")

                .params("type", String.valueOf(type))
                .params("page", page)
                .execute(new StringCallback() {
                    @Override
                    public void onBefore(BaseRequest request) {
                        super.onBefore(request);
                    }

                    @Override
                    public void onSuccess(String s, Call call, Response response) {
                       loding.setVisibility(View.GONE);
                        swipeRefresh.finishRefresh();
                        swipeRefresh.finishLoadMore();
                        Gson g = new Gson();
                        try {
                            if (type == 4) {
                                NewBookRankBean newBook = g.fromJson(s, NewBookRankBean.class);
                                if (newBook.getReturnCode() == 200) {
                                    showBeanList.addAll(newBook.getData().getNewBookRankingList().getList());
                                    mAdapter.setNewData(showBeanList);
                                } else {

                                }
                            } else {
                                RankingItem rank = g.fromJson(s, RankingItem.class);
                                if (rank.getReturnCode() == 200) {
                                    switch (zyz) {
                                        case 0:
                                            break;
                                        case 1:
                                            showBeanList.addAll(rank.getData().getWeek().getList());
                                            break;
                                        case 2:
                                            showBeanList.addAll(rank.getData().getMonth().getList());
                                            break;
                                        case 3:
                                            showBeanList.addAll(rank.getData().getTotal().getList());
                                            break;
                                    }
                                    mAdapter.setNewData(showBeanList);
                                }
                            }
                        } catch (Exception e) {
                            e.printStackTrace();
                        }

                    }
                });
    }



}
